<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="商品名称"
        prop="materialName"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.materialName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item
        label="序列号"
        prop="serialNumber"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.serialNumber" placeholder="序列号"></el-input>
      </el-form-item>
      <el-form-item
        label="备注"
        prop="remark"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    return {
      parentValue: [],
      value1: "",
      value2: "",
      options: [
        {
          value: "1",
          label: "未营业",
        },
        {
          value: "2",
          label: "正常营业",
        },
        {
          value: "3",
          label: "暂停营业",
        },
        {
          value: "4",
          label: "终止营业",
        },
        {
          value: "5",
          label: "已除名",
        },
      ],
      value: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        DepotName: "",
        serialNumber: "",
        remark: "",
        model: "",
        categoryName: "",
        materialOther: "",
        categoryName: "",
        materialOther: "",
        unitName: "",
        safetyStock: "",
        purchaseDecimal: "",
        wholesaleDecimal: "",
        commodityDecimal: "",
        lowDecimal: "",
      },
      dataRule: {
        materialName: [
          { required: true, message: "条码不能为空", trigger: "blur" },
        ],
        serialNumber: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        remark: [
          { required: true, message: "规格不能为空", trigger: "blur" },
        ],
        model: [{ required: true, message: "型号不能为空", trigger: "blur" }],
        categoryName: [
          { required: true, message: "类别不能为空", trigger: "blur" },
        ],
        materialOther: [
          { required: true, message: "扩展信息不能为空", trigger: "blur" },
        ],
        categoryName: [
          { required: true, message: "类别不能为空", trigger: "blur" },
        ],
        unitName: [
          { required: true, message: "单位不能为空", trigger: "blur" },
        ],
        safetyStock: [
          { required: true, message: "单位不能为空", trigger: "blur" },
        ],
        purchaseDecimal: [
          { required: true, message: "采购价不能为空", trigger: "blur" },
        ],
        wholesaleDecimal: [
          { required: true, message: "零售价不能为空", trigger: "blur" },
        ],
        commodityDecimal: [
          { required: true, message: "销售价不能为空", trigger: "blur" },
        ],
        lowDecimal: [
          { required: true, message: "最低售价不能为空", trigger: "blur" },
        ],
        model: [{ required: true, message: "型号不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    // init(id) {
    //   // console.log(id);
    //   if(id){
    //     this.$http({
    //     url: this.$http.goods('/materialsExtend/getDetailList'),
    //     method: "get",
    //     params:({
    //         'materialId':id,
    //       })
    //   })
    //     .then((data) => {
    //       console.log(data.data.data.rows[0]);
    //       if (data.data.code === 200) {
    //         this.dataForm = data.data.data.rows[0];
    //         this.dataForm.id = id;
    //       }
    //       this.roleList = data && data.code === 0 ? data.list : [];
    //     })
    //     .then(() => {
    //       this.visible = true;
    //     });
    //   }
    // },
    init(id) {
      // console.log(id);
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.goods('/materialsExtend/getDetailList'),
              method: "get",
              params: {
                materialId: id,
              },
            }).then(({ data }) => {
              console.log(data);
              this.dataForm = data.data.rows[0];
              this.dataForm.id=id;
            });
          }
        });
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/organization/${
                !this.dataForm.id ? "addOrganization" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              materialName: this.dataForm.materialName,
              serialNumber: this.dataForm.serialNumber,
              remark: this.dataForm.remark,
              standard: this.dataForm.standard,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
